package com.dalin.platform.loa.service.imple;

import java.util.ArrayList;
import java.util.List;

import com.dalin.common.pager.PageInfo;
import com.dalin.framework.service.imple.FrameServiceImple;
import com.dalin.platform.loa.condition.LoaOperateCondition;
import com.dalin.platform.loa.model.LoaOperateEntity;
import com.dalin.platform.loa.model.LoaOperateHeaderEntity;
import com.dalin.platform.loa.service.OperateService;
import com.dalin.platform.loa.vo.LoaOperateHeaderVo;
import com.dalin.platform.loa.vo.LoaOperateVo;

/*****************************************************************************
 * @content: 类信息                                                                                                                                *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2011-03-10 09:51:15                                            *
 * @modDate: 2011-03-10 09:51:15                                            *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class OperateServiceImple extends FrameServiceImple implements OperateService {
    public PageInfo queryLoaOperates(LoaOperateCondition cond)throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaOperateEntity> entyList = pageInfo.getDataList();
        List<LoaOperateVo> voList = new ArrayList<LoaOperateVo>();
        LoaOperateVo vo = null;
        for (LoaOperateEntity enty : entyList) {
        	vo = new LoaOperateVo();
        	vo.setOperateId(enty.getOperateId()); // 
	        vo.setParentOperateId(enty.getParentOperateId()); // 
	        vo.setMenuName(enty.getLoaMenuEntity().getMenuName()); // 
	        vo.setRequestParam(enty.getRequestParam()); // 
	        vo.setMethodName(enty.getMethodName()); // 
	        vo.setIconClsPath(enty.getIconClsPath()); // 
	        vo.setOperateTip(enty.getOperateTip()); // 
	        vo.setOperateCode(enty.getOperateCode()); // 
	        vo.setOperateName(enty.getOperateName()); // 
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

    public LoaOperateVo findLoaOperateById(Long id)throws Exception {
        LoaOperateEntity enty = (LoaOperateEntity) getDao().findObjById(LoaOperateEntity.class, id);
        LoaOperateVo vo = new LoaOperateVo();
        vo.setParentOperateId(enty.getParentOperateId()); // 
        vo.setMenuId(enty.getMenuId()); // 
        vo.setRequestParam(enty.getRequestParam()); // 
        vo.setMethodName(enty.getMethodName()); // 
        vo.setOperateDescription(enty.getOperateDescription()); // 
        vo.setIconClsPath(enty.getIconClsPath()); // 
        vo.setOperateTip(enty.getOperateTip()); // 
        vo.setOperateCode(enty.getOperateCode()); // 
        vo.setOrderNumber(enty.getOrderNumber()); // 
        vo.setOperateName(enty.getOperateName()); // 
        vo.setOperateId(enty.getOperateId()); // 
        return vo;
    }

    public void addLoaOperate(LoaOperateVo vo)throws Exception {
        LoaOperateEntity enty = new LoaOperateEntity();
        enty = getLoaOperateEntityValue(enty, vo);
        getDao().createObj(enty);
    }

    public void modifyLoaOperate(LoaOperateVo vo)throws Exception {
        Long id = vo.getOperateId();
        LoaOperateEntity enty = (LoaOperateEntity) getDao().findObjById(LoaOperateEntity.class, id);
        enty = getLoaOperateEntityValue(enty, vo);
        getDao().updateObj(enty);
    }

    public void delLoaOperatesByIds(Long[] idArray)throws Exception {
        getDao().deleteObjs(LoaOperateEntity.class, idArray);
    }

    private LoaOperateEntity getLoaOperateEntityValue(LoaOperateEntity enty, LoaOperateVo vo) {
        enty.setParentOperateId(vo.getParentOperateId()); // 
        enty.setMenuId(vo.getMenuId()); // 
        enty.setRequestParam(vo.getRequestParam()); // 
        enty.setMethodName(vo.getMethodName()); // 
        enty.setOperateDescription(vo.getOperateDescription()); // 
        enty.setIconClsPath(vo.getIconClsPath()); // 
        enty.setOperateTip(vo.getOperateTip()); // 
        enty.setOperateCode(vo.getOperateCode()); // 
        enty.setOrderNumber(vo.getOrderNumber()); // 
        enty.setOperateName(vo.getOperateName()); // 
        return enty;
    }

	public void addLoaOperateHeader(LoaOperateHeaderVo vo) throws Exception {
		LoaOperateHeaderEntity enty = new LoaOperateHeaderEntity();
		enty.setHeaderId(vo.getHeaderId());
		enty.setOperateId(vo.getOperateId());
		enty.setHeaderWidth(vo.getHeaderWidth());
		enty.setHidden(vo.getHidden());
		enty.setOrderNumber(vo.getOrderNumber());
		getDao().createObj(enty);
	}

	public void addLoaOperateHeaders(Long operateId, Long[] headerIdArray) throws Exception {
		int i = 1;
		for(Long headerId : headerIdArray) {
			LoaOperateHeaderEntity enty = new LoaOperateHeaderEntity();
			enty.setHeaderId(headerId);
			enty.setOperateId(operateId);
			enty.setHidden(1);
			enty.setOrderNumber(i += 8);
			getDao().createObj(enty);
		}
	}
}
